/* rqbal.p    - BUILD APPROVER LIST                                           */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.9 $                                                         */
/*V8:ConvertMode=NoConvert                                                    */
/* Revision: 8.6    LAST MODIFIED BY: 04/22/97  By: B. Gates          *J1Q2*  */
/* REVISION: 8.6E   LAST MODIFIED: 07/22/98   BY: *L040* Charles Yen          */
/* REVISION 8.5       LAST MODIFIED: 11/01/98  BY: *J32W* Patrick Rowan       */
/* REVISION 8.6E      LAST MODIFIED: 03/24/99  BY: *L0DQ* Poonam Bahl         */
/* REVISION 8.6E      LAST MODIFIED: 05/17/99  BY: *J3FT* Madhavi Pradhan     */
/* REVISION 9.1       LAST MODIFIED: 08/12/00  BY: *N0KP* myb                 */
/* $Revision: 1.9 $    BY: Anitha Gopal          DATE: 12/25/01  ECO: *N174*  */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

{mfdeclre.i}

define input parameter p_rqm_recid as recid no-undo.

{rqalttd.i} /*APPROVER LIST TEMPTABLE DEFINITIONS*/
{rqconst.i} /*CONSTANTS*/

define variable action_found             as   logical no-undo.
define variable category_apprcurr_total  as   decimal no-undo.
define variable category_total           as   decimal no-undo.
define variable exchg_rate               as   decimal no-undo.
define variable exchg_rate2              as   decimal no-undo.
define variable exch-error-number        like msg_nbr no-undo.
define variable mc-error-number          like msg_nbr no-undo.
define variable following_action         like rqh_action no-undo.
define variable is_approver              as   logical no-undo.
define variable is_reviewer              as   logical no-undo.
define variable prod_line_apprcurr_total as   decimal no-undo.
define variable prod_line_total          as   decimal no-undo.
define variable req_max_apprcurr_total   as   decimal no-undo.
define variable include_user             as   logical no-undo.

for each ttcal exclusive-lock:
   delete ttcal.
end. /* FOR EACH ttcal */

for each ttval exclusive-lock:
   delete ttval.
end. /* FOR EACH ttval */

for each tthal exclusive-lock:
   delete tthal.
end. /* FOR EACH tthal */

for each ttjal exclusive-lock:
   delete ttjal.
end. /* FOR EACH ttjal */

for each ttdal exclusive-lock:
   delete ttdal.
end. /* FOR EACH ttdal */

for each ttalt exclusive-lock:
   delete ttalt.
end. /* FOR EACH ttalt */

find rqm_mstr
   where recid(rqm_mstr) = p_rqm_recid
   no-lock.

/*GET CURRENT EXCHANGE RATE TO APPROVAL CURRENCY*/

find first rqf_ctrl no-lock.
{gprun.i ""rqexrt.p""
   "(input rqm_curr,
     input rqf_appr_curr,
     input rqm_ex_ratetype,
     output exchg_rate,
     output exchg_rate2,
     output exch-error-number)"}

if exch-error-number <> 0
then do:
   /* NO EXCHANGE RATE FOR APPROVAL CURRENCY */
   {pxmsg.i &MSGNUM=2087 &ERRORLEVEL=3}
   leave.
end. /* IF exch-error-number <> 0 */

{gprunp.i "mcpl" "p" "mc-curr-conv"
   "(input rqm_curr,
     input rqf_appr_curr,
     input exchg_rate,
     input exchg_rate2,
     input rqm_max_total,
     input false, /* DO NOT ROUND */
     output req_max_apprcurr_total,
     output mc-error-number)"}

if rqm_direct
then do:
   /*GET PRODUCT LINE APPROVERS*/

   for each rqd_det no-lock
      where rqd_nbr = rqm_nbr
      and rqd_status = LINE_STATUS_OPEN,
      each pt_mstr no-lock
      where pt_part = rqd_part
      break by rqd_site
            by pt_prod_line:
      if first-of(pt_prod_line)
      then do:
         prod_line_total = 0.
      end. /* IF first-of(pt_prod_line) */

      prod_line_total = prod_line_total
                      + rqd_req_qty
                      * rqd_max_cost.

      if last-of(pt_prod_line)
      then do:

         /* OBTAINING PRODUCT LINE TOTAL IN APPROVAL CURRENCY */
         {gprunp.i "mcpl" "p" "mc-curr-conv"
            "(input rqm_curr,
              input rqf_appr_curr,
              input exchg_rate,
              input exchg_rate2,
              input prod_line_total,
              input false, /* DO NOT ROUND */
              output prod_line_apprcurr_total,
              output mc-error-number)"}

         for each rqa_mstr no-lock
            where rqa_type    = DIRECT
            and rqa_site      = rqd_site
            and rqa_prod_line = pt_prod_line
            and rqa_entity    = ""
            and rqa_job       = ""
            and rqa_category  = ""
            and rqa_sub_from <= rqm_sub
            and (rqa_sub_to  >= rqm_sub or rqa_sub_to = "")
            and rqa_cc_from  <= rqm_cc
            and (rqa_cc_to   >= rqm_cc or rqa_cc_to = "")
            and (rqa_start   <= today or rqa_start = ?)
            and (rqa_end     >= today or rqa_end = ?):

            /*CHECK TO SEE IF A QUALIFIED APPROVER*/

            find rql_mstr
               where rql_level = rqa_apr_level
               and   rql_amt  >= prod_line_apprcurr_total
               no-lock no-error.

            is_approver = available rql_mstr.

            /*CHECK TO SEE IF A QUALIFIED REVIEWER*/

            find rql_mstr
               where rql_level = rqa_rvw_level
               and   rql_amt  <= prod_line_apprcurr_total
            no-lock no-error.

            is_reviewer = available rql_mstr.

            /*IF EITHER, ADD TO THE LIST*/

            if is_approver
               or is_reviewer
            then do:
               find ttdal
                  where ttdal_apr_userid = rqa_apr
                  and   ttdal_site       = rqd_site
                  and   ttdal_prod_line  = pt_prod_line
               exclusive-lock no-error.

               if not available ttdal
               then do:
                  create ttdal.

                  assign
                     ttdal_site       = rqd_site
                     ttdal_prod_line  = pt_prod_line
                     ttdal_apr_userid = rqa_apr.
               end. /* IF NOT AVAILABLE ttdal */

               /*THE FOLLOWING HANDLES WHEN THERE ARE SEVERAL
               DEFINITIONS FOR AN APPROVER*/

               if is_approver
               then do:
                  ttdal_is_approver = true.
                  if rqa_apr_req
                  then
                     ttdal_apr_req = true.
               end. /* IF is_approver */

               if is_reviewer
               then do:
                  ttdal_is_reviewer = true.
                  if rqa_rvw_req
                  then
                     ttdal_rvw_req = true.
               end. /* IF is_reviewer */

               /*ADD ALTERNATE 1 TO ALTERNATES LIST*/

               run add_to_alternates_list
                  (input rqa_apr,
                   input rqa_alt_apr[1],
                   input DIRECT,
                   input ROLE_ALTERNATE_APPROVER).

               /*ADD ALTERNATE 2 TO ALTERNATES LIST*/

               run add_to_alternates_list
                  (input rqa_apr,
                   input rqa_alt_apr[2],
                   input DIRECT,
                   input ROLE_ALTERNATE_APPROVER).

               /*ADD ADMIN TO ALTERNATES LIST*/

               run add_to_alternates_list
                  (input rqa_apr,
                   input rqa_admin_apr,
                   input DIRECT,
                   input ROLE_ADMIN_APPROVER).

               /*ADD TO COMBINED LIST*/

               run add_to_combined_list
                  (input rqa_apr,
                   input DIRECT,
                   input is_approver,
                   input is_reviewer,
                   input rqa_apr_req,
                   input rqa_rvw_req).
            end. /* IF is_approver */
         end. /* FOR EACH rqa_mstr NO-LOCK */
      end. /* IF last-of(pt_prod_line) */
   end. /* FOR EACH rqd_det NO-LOCK */
end. /* IF rqm_direct */
else do:
   /*GET HORIZONTAL APPROVERS*/

   for each rqd_det no-lock
      where rqd_nbr      = rqm_nbr
      and   rqd_status   = LINE_STATUS_OPEN
      and   rqd_category > ""
      break by rqd_category:
      if first-of(rqd_category)
      then do:
         category_total = 0.
      end. /* IF first-of(rqd_category) */

      category_total = category_total
                     + rqd_req_qty
                     * rqd_max_cost.

      if last-of(rqd_category)
      then do:

         /* OBTAINING CATEGORY TOTAL IN APPROVAL CURRENCY */
         {gprunp.i "mcpl" "p" "mc-curr-conv"
            "(input rqm_curr,
              input rqf_appr_curr,
              input exchg_rate,
              input exchg_rate2,
              input category_total,
              input false, /* DO NOT ROUND */
              output category_apprcurr_total,
              output mc-error-number)"}

         for each rqa_mstr no-lock
            where rqa_type = horizontal
            and rqa_site = ""
            and rqa_prod_line = ""
            and (rqa_entity = rqm_entity or rqa_entity = "")
            and rqa_job = ""
            and rqa_category = rqd_category
            and rqa_sub_from <= rqm_sub
            and (rqa_sub_to >= rqm_sub or rqa_sub_to = "")
            and rqa_cc_from <= rqm_cc
            and (rqa_cc_to >= rqm_cc or rqa_cc_to = "")
            and (rqa_start <= today or rqa_start = ?)
            and (rqa_end >= today or rqa_end = ?):
            /*CHECK TO SEE IF A QUALIFIED APPROVER*/

            find rql_mstr
               where rql_level = rqa_apr_level
               and   rql_amt  >= category_apprcurr_total
            no-lock no-error.

            is_approver = available rql_mstr.

            /*CHECK TO SEE IF A QUALIFIED REVIEWER*/

            find rql_mstr
               where rql_level = rqa_rvw_level
               and   rql_amt  <= category_apprcurr_total
            no-lock no-error.

            is_reviewer = available rql_mstr.

            /*IF EITHER, ADD TO THE LIST*/

            if is_approver
               or is_reviewer
            then do:
               find tthal
                  where tthal_apr_userid = rqa_apr
                  and   tthal_category   = rqd_category
               exclusive-lock no-error.

               if not available tthal
               then do:
                  create tthal.

                  assign
                     tthal_category   = rqd_category
                     tthal_apr_userid = rqa_apr.
               end. /* IF NOT AVAILABLE tthal */

               /*THE FOLLOWING HANDLES WHEN THERE ARE SEVERAL
               DEFINITIONS FOR AN APPROVER*/

               if is_approver
               then do:
                  tthal_is_approver = true.
                  if rqa_apr_req
                  then
                     tthal_apr_req = true.
               end. /* IF is_approver */

               if is_reviewer
               then do:
                  tthal_is_reviewer = true.
                  if rqa_rvw_req
                  then
                     tthal_rvw_req = true.
               end. /* IF is_reviewer */

               /*ADD ALTERNATE 1 TO ALTERNATES LIST*/

               run add_to_alternates_list
                  (input rqa_apr,
                   input rqa_alt_apr[1],
                   input horizontal,
                   input ROLE_ALTERNATE_APPROVER).

               /*ADD ALTERNATE 2 TO ALTERNATES LIST*/

               run add_to_alternates_list
                  (input rqa_apr,
                   input rqa_alt_apr[2],
                   input horizontal,
                   input ROLE_ALTERNATE_APPROVER).

               /*ADD ADMIN TO ALTERNATES LIST*/

               run add_to_alternates_list
                  (input rqa_apr,
                   input rqa_admin_apr,
                   input horizontal,
                   input ROLE_ADMIN_APPROVER).

               /*ADD TO COMBINED LIST*/

               run add_to_combined_list
                  (input rqa_apr,
                   input horizontal,
                   input is_approver,
                   input is_reviewer,
                   input rqa_apr_req,
                   input rqa_rvw_req).

            end. /* IF is_approver */
         end. /* FOR EACH rqa_mstr NO-LOCK */
      end. /* IF last-of(rqd_category) */
   end. /* fOR EACH rqd_det NO-LOCK */

   /*GET JOB APPROVERS IF JOB IDENTIFIED IN REQUISITION*/

   if rqm_job > ""
   then do:
      for each rqa_mstr no-lock
         where rqa_type      = JOB
         and   rqa_site      = ""
         and   rqa_prod_line = ""
         and   (rqa_entity   = rqm_entity
               or rqa_entity = "")
         and   rqa_job       = rqm_job
         and   rqa_category  = ""
         and   rqa_sub_from <= rqm_sub
         and   (rqa_sub_to  >= rqm_sub
               or rqa_sub_to = "")
         and   rqa_cc_from  <= rqm_cc
         and   (rqa_cc_to   >= rqm_cc
               or rqa_cc_to  = "")
         and   (rqa_start   <= today
               or rqa_start  = ?)
         and   (rqa_end     >= today
               or rqa_end    = ?):

         /*CHECK TO SEE IF A QUALIFIED APPROVER*/

         find rql_mstr
            where rql_level = rqa_apr_level
            and   rql_amt  >= req_max_apprcurr_total
         no-lock no-error.

         is_approver = available rql_mstr.

         /*CHECK TO SEE IF A QUALIFIED REVIEWER*/

         find rql_mstr
            where rql_level = rqa_rvw_level
            and   rql_amt  <= req_max_apprcurr_total
         no-lock no-error.

         is_reviewer = available rql_mstr.

         /*IF EITHER, ADD TO THE LIST*/

         if is_approver
            or is_reviewer
         then do:
            find ttjal
               where ttjal_apr_userid = rqa_apr
            exclusive-lock no-error.

            if not available ttjal
            then do:
               create ttjal.

                  ttjal_apr_userid = rqa_apr.
            end. /* IF NOT AVAILABLE ttjal */

            /*THE FOLLOWING HANDLES WHEN THERE ARE SEVERAL
            DEFINITIONS FOR AN APPROVER*/

            if is_approver
            then do:
               ttjal_is_approver = true.
               if rqa_apr_req
               then
                  ttjal_apr_req = true.
            end. /* IF is_approver */

            if is_reviewer
            then do:
               ttjal_is_reviewer = true.
               if rqa_rvw_req
               then
                  ttjal_rvw_req = true.
            end. /* IF is_reviewer */

            /*ADD ALTERNATE 1 TO ALTERNATES LIST*/

            run add_to_alternates_list
               (input rqa_apr,
                input rqa_alt_apr[1],
                input JOB,
                input ROLE_ALTERNATE_APPROVER).

            /*ADD ALTERNATE 2 TO ALTERNATES LIST*/

            run add_to_alternates_list
               (input rqa_apr,
                input rqa_alt_apr[2],
                input JOB,
                input ROLE_ALTERNATE_APPROVER).

            /*ADD ADMIN TO ALTERNATES LIST*/

            run add_to_alternates_list
               (input rqa_apr,
                input rqa_admin_apr,
                input JOB,
                input ROLE_ADMIN_APPROVER).

            /*ADD TO COMBINED LIST*/

            run add_to_combined_list
               (input rqa_apr,
                input JOB,
                input is_approver,
                input is_reviewer,
                input rqa_apr_req,
                input rqa_rvw_req).

         end. /* IF is_approver */
      end. /* FOR EACH rqa_mstr NO-LOCK */
   end. /* IF rqm_job > "" */

   /*GET VERTICAL APPROVERS*/

   for each rqa_mstr no-lock
      where rqa_type      = vertical
      and   rqa_site      = ""
      and   rqa_prod_line = ""
      and   (rqa_entity   = rqm_entity
            or rqa_entity = "")
      and   rqa_job       = ""
      and   rqa_category  = ""
      and   rqa_sub_from <= rqm_sub
      and   (rqa_sub_to  >= rqm_sub
            or rqa_sub_to = "")
      and   rqa_cc_from  <= rqm_cc
      and   (rqa_cc_to   >= rqm_cc
            or rqa_cc_to  = "")
      and   (rqa_start   <= today
            or rqa_start  = ?)
      and   (rqa_end     >= today
            or rqa_end    = ?):

      /*CHECK TO SEE IF A QUALIFIED APPROVER*/

      find rql_mstr
         where rql_level = rqa_apr_level
         and   rql_amt  >= req_max_apprcurr_total
      no-lock no-error.

      is_approver = available rql_mstr.

      /*CHECK TO SEE IF A QUALIFIED REVIEWER*/

      find rql_mstr
         where rql_level = rqa_rvw_level
         and   rql_amt  <= req_max_apprcurr_total
      no-lock no-error.

      is_reviewer = available rql_mstr.

      /*IF EITHER, ADD TO THE LIST*/

      if is_approver
         or is_reviewer
      then do:
         find ttval
            where ttval_apr_userid = rqa_apr
         exclusive-lock no-error.

         if not available ttval
         then do:
            create ttval.

               ttval_apr_userid = rqa_apr.
         end. /* IF NOT AVAILABLE ttval */

         /*THE FOLLOWING HANDLES WHEN THERE ARE SEVERAL
         DEFINITIONS FOR AN APPROVER*/

         if is_approver
         then do:
            ttval_is_approver = true.
            if rqa_apr_req
            then
               ttval_apr_req = true.
         end. /* IF is_approver */

         if is_reviewer
         then do:
            ttval_is_reviewer = true.
            if rqa_rvw_req
            then
               ttval_rvw_req = true.
         end. /* IF is_reviewer */

         /*ADD ALTERNATE 1 TO ALTERNATES LIST*/

         run add_to_alternates_list
            (input rqa_apr,
             input rqa_alt_apr[1],
             input vertical,
             input ROLE_ALTERNATE_APPROVER).

         /*ADD ALTERNATE 2 TO ALTERNATES LIST*/

         run add_to_alternates_list
            (input rqa_apr,
             input rqa_alt_apr[2],
             input vertical,
             input ROLE_ALTERNATE_APPROVER).

         /*ADD ADMIN TO ALTERNATES LIST*/

         run add_to_alternates_list
            (input rqa_apr,
             input rqa_admin_apr,
             input vertical,
             input ROLE_ADMIN_APPROVER).

         /*ADD TO COMBINED LIST*/

         run add_to_combined_list
            (input rqa_apr,
             input vertical,
             input is_approver,
             input is_reviewer,
             input rqa_apr_req,
             input rqa_rvw_req).

      end. /* IF is_approver */
   end. /* FOR EACH rqa_mstr NO-LOCK */
end. /* ELSE DO */

/*FOR BOTH DIRECT AND INDIRECT:
A) ADD ANYONE TO THE COMBINED LIST WHO MAY HAVE APPROVED/DENIED
BUT WHO ISN'T IN THE APPROVERS TABLE.
B) MARK THE COMBINED LIST RECORDS WITH THE DATE/TIME OF ANY
APPROVAL/DENIAL.*/

for each rqda_det no-lock
   where rqda_nbr = rqm_nbr:

   find ttcal
      where ttcal_apr_userid = rqda_apr_userid
   exclusive-lock no-error.

   if not available ttcal
   then do:
      create ttcal.

      assign
         ttcal_apr_userid = rqda_apr_userid
         ttcal_required   = false.
   end. /* IF NOT AVAILABLE ttcal */

   assign
      ttcal_apr_date   = rqda_date
      ttcal_apr_time   = rqda_time
      ttcal_apr_action = rqda_action.
end. /* FOR EACH rqda_det NO-LOCK */

/******************************************************/
/******************************************************/
/**                 PROCEDURES                       **/
/******************************************************/
/******************************************************/

PROCEDURE add_to_combined_list:
   define input parameter p_apr         like rqa_apr no-undo.
   define input parameter p_apr_type    like rqa_type no-undo.
   define input parameter p_is_approver as log no-undo.
   define input parameter p_is_reviewer as log no-undo.
   define input parameter p_apr_req     as log no-undo.
   define input parameter p_rvw_req     as log no-undo.

   find ttcal
      where ttcal_apr_userid = p_apr
   exclusive-lock no-error.

   if not available ttcal
   then do:
      create ttcal.
      ttcal_apr_userid = p_apr.
   end. /* IF NOT AVAILABLE ttcal */

   assign
      ttcal_is_horizontal = p_apr_type = horizontal
      ttcal_is_vertical   = p_apr_type = vertical
      ttcal_is_job        = p_apr_type = JOB.

   if p_is_approver
   then
      ttcal_is_approver = true.
   if p_is_reviewer
   then
      ttcal_is_reviewer = true.

   if (p_is_approver and p_apr_req) or
      (p_is_reviewer and p_rvw_req)
   then
      ttcal_required = true.

END PROCEDURE. /* add_to_combined_lis */

PROCEDURE add_to_alternates_list:
   define input parameter p_apr_userid like rqa_apr no-undo.
   define input parameter p_alt_userid like rqa_alt_apr[1] no-undo.
   define input parameter p_apr_type   like rqa_type no-undo.
   define input parameter p_role       like rqh_role no-undo.

   if p_alt_userid = ""
   then
      leave.

   find ttalt
      where ttalt_apr_userid = p_apr_userid
      and   ttalt_alt_userid = p_alt_userid
   exclusive-lock no-error.

   if not available ttalt
   then do:
      create ttalt.

      assign
         ttalt_apr_userid = p_apr_userid
         ttalt_alt_userid = p_alt_userid.
   end. /* IF NOT AVAILABLE ttalt */

   if p_apr_type = horizontal
   then
      ttalt_is_horizontal = true.
   else if p_apr_type = vertical
   then
      ttalt_is_vertical = true.
   else if p_apr_type = JOB
   then
      ttalt_is_job = true.
   else if p_apr_type = DIRECT
   then
      ttalt_is_direct = true.

   if p_role = ROLE_ALTERNATE_APPROVER
   then
      ttalt_is_alt_apr = true.
   else if p_role = ROLE_ADMIN_APPROVER
   then
      ttalt_is_admin_apr = true.

END PROCEDURE. /* add_to_alternates_list */
